<?php
/**
 *
 * @category    API
 * @package     Api_Partner
 * @subpackage  Model
 * @author      trungpm
 */
class Api_Model_Partner {

    /*
     * Get objects by conditions author: datnh
     */
    const table_name = 'tblvgt_partner';

    /*
     * Insert a new object and return key huudatxm
     */
    function insert($data, $user, $password) {
        $Errors = array ();
        $db = db ( $user, $password );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }

        $FieldNames = '';
        $FieldValues = '';
        foreach ( $data as $key => $value ) {
            $FieldNames .= $key . ", ";
            $FieldValues .= 'N\'' . $value . '\', ';
        }
        $FieldNames .= 'created_date, ';
        $FieldValues .= 'N\'' . date ( 'Y-m-d' ) . '\', ';
        $FieldNames .= 'created_by, ';
        $FieldValues .= 'N\'' . $_SESSION ['UserID'] . '\', ';
        $FieldNames .= 'updated_date, ';
        $FieldValues .= 'N\'' . date ( 'Y-m-d' ) . '\', ';
        $FieldNames .= 'updated_by, ';
        $FieldValues .= 'N\'' . $_SESSION ['UserID'] . '\', ';

        $sql = 'INSERT INTO tblvgt_partner (' . substr ( $FieldNames, 0, - 2 ) . ') ' . ' VALUES (' . substr ( $FieldValues, 0, - 2 ) . ') ';
        if (sizeof ( $Errors ) == 0) {
            DB_Query ( $sql, $db );
            return $_SESSION ['LastInsertId'];
        }
        return 0;
    }

    /*
     * update huudatxm
     */
    function update($data, $user, $password) {
        $Errors = array ();
        $db = db ( $user, $password );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }

        $val_str = $data ['field'] . ' = \'' . $data ['value'] . '\'';
        $val_str .= ', updated_date = \'' . date ( 'Y-m-d' ) . '\' ';
        $val_str .= ', updated_by = \'' . $_SESSION ['UserID'] . '\' ';

        $sql = 'UPDATE tblvgt_partner SET ' . $val_str;
        $sql .= ' WHERE partner_id = ' . $data ['partner_id'];

        if (sizeof ( $Errors ) == 0) {
            return DB_Query ( $sql, $db );
        }
        return 0;
    }

    /*
     * delete huudatxm
     */
    function delete($partner_id, $user, $password) {
        $Errors = array ();
        $db = db ( $user, $password );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }

        $checkId = $this->verifyFieldExist ( 'partner_id', $partner_id );
        if ($checkId == 1) {
            $val_str = 'locked = 1';
            $val_str .= ', updated_date = N\'' . date ( 'Y-m-d' ) . '\' ';
            $val_str .= ', updated_by = N\'' . $_SESSION ['UserID'] . '\' ';
            $sql = "UPDATE " . self::table_name . " SET $val_str WHERE partner_id = $partner_id";

            if (sizeof ( $Errors ) == 0) {
                return DB_Query ( $sql, $db );
            }
        }
        return null;
    }

    /*
     * author: datnh
     */
    function GetAllPartner($user, $password) {
        $db = db ( $user, $password );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        $SongName = DB_escape_string ( $SongName );
        $sql = 'SELECT partner_id,    partner_number, partner_name
                FROM tblvgt_partner';

        $result = DB_Query ( $sql, $db );

        if (DB_has_rows ( $result ) == 0) {
            return null;
        } else {
            return $result;
        }
        DB_free_result ( $result );
    }

    /*
     * author: trungpm
     */
    function GetPartnerById($PartnerId, $user, $password) {
        $db = db ( $user, $password );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        $sql = 'SELECT partner_id,
                    partner_name
                FROM tblvgt_partner WHERE partner_id=N\'' . $PartnerId . '\'';
        $result = DB_Query ( $sql, $db );
        $arrResult = DB_fetch_array ( $result );
        DB_free_result ( $result );
        return $arrResult;
    }

    /*
     * Verify field existed.
     */
    function verifyFieldExist($field, $value) {
        // Check user permission
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        ;
        $Searchsql = "SELECT count(*) as c FROM " . self::table_name . " WHERE $field = N'$value'";
        $SearchResult = DB_query ( $Searchsql, $db );
        $answer = DB_fetch_array ( $SearchResult );
        if ($answer ['c'] != 0) {
            return 1;
        }
        ;
        return 0;
    }

    /**
     * load datas with paginates
     */
    function getPartnerPaginates($limit, $start, &$total, $conds) {
        // Check user permission
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        // Get search conditions
        $condsStr = null;
        if (! empty ( $conds )) {
            $arr = array ();
            foreach ( $conds as $cond ) {
                $arr [] = $cond ['field'] . ' ' . $cond ['relation'] . ' ' . $cond ['value'];
            }
            $condsStr .= implode ( " AND ", $arr );
        }

        $where = '';
        if (! empty ( $condsStr )) {
            $where = 'WHERE ' . $condsStr;
        }
        // Get query string for size
        $sql = "SELECT COUNT(*) AS size
                FROM " . self::table_name . " $where";
        // Get size
        $result = DB_Query ( $sql, $db );
        $obj = DB_fetch_object ( $result );
        $total = intval ( $obj->size );
        DB_free_result ( $result );

        // Get query string with row_num
        $query = "SELECT @rownum:=@rownum+1 AS rownum,
                    partner_id,
                    partner_number ,
                    partner_name,
                    partner_address,
                    partner_phone,
                    partner_fax,
                    partner_email,
                    partner_website
                FROM " . self::table_name . "
                JOIN (SELECT @rownum:=0) r ON 1 = 1
                $where";

        // Get query string with paginate
        if (isset ( $start ) && isset ( $limit )) {
            $query .= " LIMIT $start, $limit";
        }
        // Execute query
        $result = DB_Query ( $query, $db );
        // Return result
        if (DB_has_rows ( $result ) == 0) {
            return null;
        } else {
            return $result;
        }
    }
}

?>
